#if UNITY_ANDROID
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;

namespace ZhiSe.Editor
{
    internal class SeegAndroidBuild
    {
        public static void OnPostprocessBuild(string gid, string pf, JObject cfg)
        {
            var resDir = $"{SeegUtil.GetCfgDir()}/{(gid.EndsWith("_") ? pf : gid)}";
            var sdkParamPath = $"{resDir}/seeg_sdk_param.json";
            if (!File.Exists(sdkParamPath))
            {
                Debug.Log($"[SEEG] seeg_sdk_param.json文件不存在，请检查路径：{sdkParamPath}");
                return;
            }
            var sdkParamObj = JObject.Parse(File.ReadAllText(sdkParamPath));
            JObject info;
            if (!sdkParamObj.ContainsKey("info"))
            {
                info = new JObject();
            }
            else
            {
                info = (JObject)sdkParamObj["info"];
            }
            foreach (var item in cfg)
            {
                info![item.Key] = item.Value;
            }
            sdkParamObj["info"] = info;
            // 如果不存在assets文件夹，则创建
            var assetsPath = $"{SeegBuild.OutputPath}/launcher/src/main/assets";
            if (!Directory.Exists(assetsPath))
            {
                Directory.CreateDirectory(assetsPath);
            }
            // 拷贝seeg_sdk_param.json到assets文件夹
            File.WriteAllText($"{assetsPath}/seeg_sdk_param.json", sdkParamObj.ToString(Formatting.None));
            // 拷贝google-services.json到launcher文件夹
            var firebasePath = $"{resDir}/google-services.json";
            if (File.Exists(firebasePath))
            {
                File.Copy($"{resDir}/google-services.json", $"{SeegBuild.OutputPath}/launcher/google-services.json", true);
            }
        }

    }
}
#endif